package com.sctvcloud.qingchuan.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.luck.picture.lib.PictureExternalPreviewActivity;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.ruihang.generalibrary.ui.adapter.BaseHolderAbsAdapter;
import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.beans.PoliticsInfoImage;
import com.sctvcloud.qingchuan.ui.adapter.holder.PoliticsImgHolder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 问政列表、详情中九宫格图片显示
 */
public class PoliticsItemImgAdapter extends BaseHolderAbsAdapter<PoliticsInfoImage, PoliticsImgHolder> {

    public PoliticsItemImgAdapter(Context context, List<PoliticsInfoImage> list) {
        super(context, list);
    }

    @Override
    public PoliticsImgHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_politics_img, parent, false);
        PoliticsImgHolder picHolder = new PoliticsImgHolder(context, view);
        return picHolder;
    }

    @Override
    public int getItemCount() {
        if (data.size() > 9) {
            return 9;
        } else {
            return data.size();
        }
        //return super.getItemCount();
    }

    @Override
    public void onBindViewHolder(PoliticsImgHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<LocalMedia> images = new ArrayList<LocalMedia>();
                for (PoliticsInfoImage imageUr : data) {
                    LocalMedia localMedia = new LocalMedia();
                    localMedia.setPath(imageUr.getImagePath());
                    images.add(localMedia);
                }
                Intent intent = new Intent(context, PictureExternalPreviewActivity.class);
                intent.putExtra("type", 1);
                intent.putExtra(PictureConfig.EXTRA_PREVIEW_SELECT_LIST, (Serializable) images);
                intent.putExtra(PictureConfig.EXTRA_POSITION, position);
                context.startActivity(intent);
            }
        });
    }
}
